package training.bms.presentation;

import java.util.List;

import org.apache.commons.lang.ObjectUtils;

import training.bms.business.Blog;
import training.bms.business.BlogController;
import training.bms.business.BlogSearchOptions;
import training.bms.business.Post;
import training.framework.presentation.AbstractEntityPanel;

public class PostPanel extends AbstractEntityPanel<Integer, Post>{
	private List<Blog> blogs;
	
	public PostPanel() {
		this(new Post());
	}

	public PostPanel(Post post) {
		setEntity(post);
		this.blogs = searchBlogs();		
	}
		
	private List<Blog> searchBlogs() {
		BlogSearchOptions options = new BlogSearchOptions();
		BlogController controller = new BlogController();
		return controller.searchEntity(options);
	}	
	
	public List<Blog> getBlogs() {
		return blogs;
	}
	
	public void setBlogs(List<Blog> blogs) {
		this.blogs = blogs;
	}
	
	public void setBlogIdentifier(Integer identifier) {
		Blog blog = searchBlog(identifier);
		getEntity().setBlog(blog);
	}
	
	private Blog searchBlog(Integer identifier) {
		for (Blog blog : blogs) {
			if (ObjectUtils.equals(blog.getIdentifier(), identifier)) {
				return blog;
			}
		}
		return null;
	}

	public Integer getBlogIdentifier() {
		Blog blog = getEntity().getBlog();
		if (blog == null) {
			return null;
		} else {
			return blog.getIdentifier();
		}
	}	
	
	
}
